package com;

import tool.jdbctool;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet(name = "core")
public class core extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session=request.getSession();
        response.setContentType("text/html;charset=utf-8");
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        try {
            connection= jdbctool.getconection();

            statement=connection.prepareStatement("update `servlet`.`users`\n" +
                    "set `core` = ?\n" +
                    "where `userid` = ?");
            statement.setString(1, String.valueOf(session.getAttribute("core")));
            statement.setString(2, String.valueOf(session.getAttribute("userid")));
            statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            jdbctool.close(connection,statement,resultSet);
        }
        PrintWriter out=response.getWriter();
        System.out.println(session.getAttribute("userid"));
        out.write("<font style=\"color: red;font-size: 40px\">您答对了"+Integer.valueOf(String.valueOf(session.getAttribute("core")))/25+"道题，获得了分数："+session.getAttribute("core")+"</font>");
    }
}
